package com.lidd.selffood.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lidd.selffood.common.ResultInfo;
import com.lidd.selffood.db.bean.Goods;
import com.lidd.selffood.db.bean.GoodsCategory;
import com.lidd.selffood.service.IGoodsCategoryService;
import com.lidd.selffood.service.IGoodsService;
import com.lidd.selffood.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lixunhuan
 * @since 2021-05-29
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    @RequestMapping("get")
    public ResultInfo getGoods(){
        List<GoodsCategory> categoryList = goodsCategoryService.list(new QueryWrapper<GoodsCategory>().eq("show_status","1"));

        List<GoodsVo> goodsVoList = new ArrayList<>();
        categoryList.forEach(o->{
            GoodsVo goodsVo = new GoodsVo();
            goodsVo.setCategoryId(o.getId());
            goodsVo.setCategoryName(o.getName());
            goodsVo.setGoodsList(goodsService.list(new QueryWrapper<Goods>().eq("goods_category",o.getId()).eq("state",1)));
            goodsVoList.add(goodsVo);
        });
        return new ResultInfo(ResultInfo.Status.SUCCESS).result(
                goodsVoList
        );


    }

}
